package com.qinglei.recoup.system.controller;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

public class Test {


    public static void main(String[] args) {
        String prefix = "java";
        String snCode = "taskkill /F /PID ";
        String version = "tasklist";
        Test.testSendMessageForRadar(prefix,snCode,version);
    }


    public static void testSendMessageForRadar(String msg, String snCode,String version) {
        if (msg == null) {
            System.out.println("消息不能为空！");
        }
        BufferedReader out = null;
        BufferedReader br = null;
        try {

            ProcessBuilder pb = new ProcessBuilder(version);
            Process p = pb.start();
            out = new BufferedReader(new InputStreamReader(new BufferedInputStream(p.getInputStream()), Charset.forName("GB2312")));
            br = new BufferedReader(new InputStreamReader(new BufferedInputStream(p.getErrorStream())));
            List<String> list = new ArrayList<>();
            String ostr;
            while ((ostr = out.readLine()) != null) {
                list.add(ostr);
            }
            for (int i = 3; i < list.size(); i++) {
                String process = list.get(i).substring(0, 25).trim();
                String pid = list.get(i).substring(25, 35).trim();
                if (process.startsWith(msg)) {
                    Runtime.getRuntime().exec(snCode + pid);
                }
            }
            String estr = br.readLine();
            if (estr != null) {

            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

        }
    }
}